package cn.edu.scujcc.helloworld;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class ChannelService {
	@Autowired
	private ChannelRepository repo;
	
	public Channel getChannel(String cid) {
		Channel result=null;
		if (cid!=null) {
			result = new Channel();
			if(repo.findById(cid).isPresent()) {
				result = repo.findById(cid).get();
			}
			
		}
		return result;
	}
	
	
	public List<Channel> getALLChannel() {
		List<Channel> result = null;
		result = repo.findAll();
		return result;
	}
	
	
	public boolean deleteChannel(String id) {
		boolean result = false;
		try {
		repo.deleteById(id);
		} catch (Exception e) {
			result = false;
		}
		
		return result;
	}
	
	public Channel crateChannel(Channel c) {
		return repo.save(c);
	}
	
	public Channel updateChannel(Channel c) {
		Channel result = null;
		result = getChannel(c.getId());
		if(c.getName()!=null) {
			result.setName(c.getName());
		}
		if(c.getDesc()!=null) {
			result.setDesc(c.getDesc());
		}
		repo.save(result);
		return result;
	}
	
	
	public List<Channel> searchByDesc(String desc){
		return repo.findByDescLike(desc);
	}
	
	public List<Channel> searchByName(String name){
		return repo.findByName(name);
	}
	
}
